package com.hxxy.wyp.petsLover.common.rest.base.backend;

import com.hxxy.wyp.petsLover.RestBase;
import com.hxxy.wyp.petsLover.common.services.base.backend.FileService;
import com.hxxy.wyp.petsLover.exception.AppException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ClassName FileUploadController
 * @Description 文件上传接口
 * @Author wyp
 * @Date 2022/7/30 9:04
 * @Version 1.0
 */
@RestController
@RequestMapping("/admin/file")
@ResponseBody
public class FileUploadController extends RestBase {

    @Autowired
    private FileService fileService;


    @PostMapping("upload")
    public Object uploadFile(MultipartFile file){
        String upload = fileService.upload(file);
        return ajaxSuccess(upload,"200","上传成功!");
    }

    @PostMapping("uploadMore")
    public Object uploadFileMore(MultipartFile[] files, HttpServletRequest  request){
        if (files==null||files.length<1){
            throw new AppException("请上传多个文件!");
        }
        List<String> uploads = fileService.uploads(files);
        return ajaxSuccess(uploads,"200","上传成功!");
    }

}